
let fchange = function() { /* */ }

function createFileInput() {
  const f = document.createElement('input')
  f.type = 'file';
  f.style.display = 'none'
  f.addEventListener('change', e => fchange(e))
  return f;
}

let f = createFileInput()

const pick = function(type, multiple) {
  return new Promise(resolve => {
    if (/image/.test(type)) {
      f.accept = 'image/jpeg,image/jpg,image/png,image/gif'
    } else if (/video/.test(type)) {
      f.accept = 'video/mp4,video/avi,video/rmvb,video/flv,video/mkv'
    } else {
      f.accept = '';
    }
    if (multiple) f.multiple = 'multiple'
    fchange = function(e) {
      if (multiple) resolve(e.target.files)
      else resolve(e.target.files.item(0))
      f = createFileInput()
        // console.log(window.URL.createObjectURL(file))
    }
    f.click();
  })
}

pick.toBase64 = function(url, size) {
  return new Promise((resolve, reject) => {
    const img = new Image()
    img.src = url
    img.crossOrigin = '*'
    img.onload = function() {
      const canvas = document.createElement('canvas')
        // canvas.style.display = "none"
        // document.body.appendChild(canvas)
      if (typeof size == 'function') {
        size = size(img.width, img.height)
        canvas.width = size.w
        canvas.height = size.h
      } else if (typeof size == 'object') {
        canvas.width = size.w
        canvas.height = size.h
      } else {
        canvas.width = img.width
        canvas.height = img.height
      }
      const ctx = canvas.getContext('2d')
      ctx.drawImage(img, 0, 0, canvas.width, canvas.height)
      resolve(canvas.toDataURL())
        // document.body.removeChild(canvas)
    }
    img.onerror = function() {
      reject()
    }
  })
}

export default pick